import java.io.Serializable;

/**
 * Stores location information for the various landmarks in the world. Handles
 * the movement of the wagon in the game and on the map.
 *
 */
public class Map implements Serializable
{
	private int location; //location = position in trail, measured in miles
	private int sinceLastLandmark; //toNextLandmark = distance traveled from last landmark
	//public static final int[] Landmark_Locations = {0, 102, 83, 119, 250, 86, 190, 102, 125, 162, 57, 182, 114, 162, 55, 120};
	
	// initialize landmarks; indices should correspond to indices in Landmark_Locations
	public static final Landmark[] Landmarks = {
			new Landmark("Independence", true, 102, 724, 468),
			new River("Kansas River", false, 6, 10, true, 83, 685, 452),
			new River("Big Blue", false, 2, 4, false, 119, 670, 425),
			new Landmark("Fort Kearney", true, 250, 628, 421),
			new Landmark("Chimney Rock", false, 86, 578, 411),
			new Landmark("Fort Laramie", true, 190, 518, 387),
			new Landmark("Independence Rock", false, 102, 464, 349),
			new Landmark("South Pass", false, 125, 422, 369),
			new Landmark("Fort Bridger", true, 162, 379, 426),
			new Landmark("Soda Springs", false, 57, 364, 367),
			new Landmark("Fort Hall", true, 182, 321, 340),
			new River("Snake River Crossing", false, 4, 8, false, 114, 261, 308),
			new Landmark("Fort Boise", true, 162, 243, 270),
			new Landmark("Blue Mountains", false, 55, 206, 227),
			new Landmark("Fort Walla Walla", true, 120, 201, 180),
			new Landmark("The Dalles", false, 210, 174, 199),
			new Landmark("Oregon City", false, 999, 134, 181)}; 
	private int landmarkIndex;
	private int END_LOCATION_INDEX;
	
	/**
	 * Constructor for the map class. Initializes instance variables to defaults. All you should ever need.
	 */
	public Map()
	{
		location = 0;
		landmarkIndex = 0;
		
		END_LOCATION_INDEX = Landmarks.length - 1;
	}
	
	/**
	 * Allows movement of the wagon down the trail. Stops at landmarks.
	 * @param n Number of miles to move.
	 */
	public void move(int n)
	{
		//FIXME Small bug: when wagon arrives *exactly* at a landmark, takes one extra "move" click to actually trigger
		//		"arriving" at the landmark and the dialog to appear.
		
		location += n;
		sinceLastLandmark += n;
		
		int distBetweenLM = Landmarks[landmarkIndex].getDistBetweenLandmarks();
		
		if(sinceLastLandmark >= distBetweenLM)
		{
			
			location += (distBetweenLM - sinceLastLandmark);
			landmarkIndex++;
			sinceLastLandmark = 0;			
		}

	}

	/**
	 * Accessor for the location field. Number of miles traveled down the trail so far.
	 * @return int value of the location field. 
	 */
	public int getLocation() 
	{
		return location;
	}
	
	/**
	 * Accessor for the toNextLandmark field. Number of miles until the next landmark.
	 * @return int value of Next landmark location - current location
	 */
	public int getToNextLandmark()
	{
		return (Landmarks[landmarkIndex].getDistBetweenLandmarks() - sinceLastLandmark);
	}

	/**
	 * Gets the index of the most recently visited landmark. Shouldn't need this method.
	 * @return int value of the most recent landmark index
	 */
	public int getLandmarkIndex() 
	{
		return landmarkIndex;
	}
	
	
	public boolean thereYet(){
		if (landmarkIndex == END_LOCATION_INDEX){
			return true;
		}
		else {
			return false;
		}
	}
	
	public int getSinceLastLandmark(){
		return sinceLastLandmark;
	}
	
	public boolean atRiver(){
		return (River.class.isAssignableFrom(Landmarks[landmarkIndex].getClass()));
	}
}
